$(function () {
    // 点击表单 uploadForm 的点击按钮时，触发 upload 函数
    $("#uploadForm").submit(upload);

    $('#uploadInfoForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            nickName: {
                message: '昵称验证失败',
                validators: {
                    notEmpty: {
                        message: '昵称不能为空！'
                    }
                }
            }
        }
    });

    $('#updatePasswordForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            oldPassword: {
                message: '旧密码验证失败',
                validators: {
                    notEmpty: {
                        message: '旧密码不能为空！'
                    }
                }
            },
            newPassword: {
                message: '新密码验证失败',
                validators: {
                    notEmpty: {
                        message: '新密码不能为空！'
                    },
                    regexp: {
                        regexp: /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{8,16}$/,
                        message: '新密码长度为8至16位,并且需要包含数字、小写字母、大写字母、符号(至少三种)'
                    },
                    identical: {
                        field: 'confirmPassword',
                        message: '新密码与确认密码不一致！'
                    },
                    stringLength: {
                        min: 8,
                        max: 50,
                        message: '新密码长度不能少于8位！'
                    }
                }
            },
            confirmPassword: {
                validators: {
                    notEmpty: {
                        message: '确认密码不能为空！'
                    },
                    regexp: {
                        regexp: /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{8,16}$/,
                        message: '确认密码长度为8至16位,并且需要包含数字、小写字母、大写字母、符号(至少三种)！'
                    },
                    identical: {
                        field: 'newPassword',
                        message: '新密码与确认密码不一致！'
                    },
                    stringLength: {
                        min: 8,
                        max: 50,
                        message: '确认密码长度在8-50之间！'
                    }
                }
            }
        }
    });

});

//修改基本信息提交表单数据
$('#uploadInfoBtn').click(function (event) {
    $('#uploadInfoForm').data('bootstrapValidator').validate();
    if ($('#uploadInfoForm').data('bootstrapValidator').isValid()) {
        var nickName = $("#nickName").val().trim();
        var jsonStr = {"nickName": nickName};
        $.ajax({
            url: "/user/updateInfo",
            method: "post",
            data: jsonStr,
            success: function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            },
            error: function (e) {
                alert("错误！！");
            }
        });
    }
});

//修改密码提交表单
$('#updatePasswordBtn').click(function (event) {
    $('#updatePasswordForm').data('bootstrapValidator').validate();
    if ($('#updatePasswordForm').data('bootstrapValidator').isValid()) {
        var oldPassword = $("#old-password").val().trim();
        var newPassword = $("#new-password").val().trim();
        var jsonStr = {"oldPassword": oldPassword, "newPassword": newPassword};
        $.ajax({
            url: "/user/password",
            method: "post",
            data: jsonStr,
            success: function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            },
            error: function (e) {
                alert("错误！！");
            }
        });
    }
});

function upload() {
    $.ajax({
        url: "/upload/files",
        method: "post",
        processData: false, // 不要把表单内容转换成字符串
        contentType: false, // 不要 Jquery 自动设置上传的类型
        data: new FormData($("#uploadForm")[0]),

        success: function (res) {
            console.log(res)
            if (res.code === 0) {
                // 更新头像访问路径
                $.post(
                    CONTEXT_PATH + "/user/header/url",
                    {
                        "url": res.src
                    },
                    function (data) {
                        data = $.parseJSON(data);
                        if (data.code === 0) {
                            alert(data.msg);
                            setTimeout(function () {
                                window.location.reload();
                            }, 1000);
                        }
                        else {
                            alert(data.msg)
                        }
                    }
                )
            }
            else {
                alert(res.msg);
            }
        }

    });
    return false;
}

